package com.siruier.boss.ui.activity.user

import android.app.Activity
import android.view.View
import com.siruier.boss.R
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.loginApi
import com.siruier.boss.api.mallApi
import com.siruier.boss.common.handleLoginName
import com.siruier.boss.databinding.ActivityUserCenterBinding
import com.siruier.boss.ui.activity.login.ForgetPasswordActivity
import com.siruier.boss.ui.activity.login.LoginActivity
import com.siruier.boss.ui.activity.mall.ReceivingAddressActivity
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.cache.UserCache
import com.siruier.boss.ui.dialog.InputDialog
import com.siruier.boss.ui.dialog.showAlertDialog
import com.siruier.boss.ui.ext.startActivityExt
import com.siruier.boss.ui.tool.image.select.imageSelect
import com.siruier.boss.ui.tool.image.select.service.intercept.ApiUploadFileIntercept
import com.siruier.boss.ui.utils.GlideUtils
import com.siruier.boss.ui.utils.SPUtils

class UserCenterActivity : BaseActivity<ActivityUserCenterBinding>() {
    override fun initView() {
        bindViewClick(
            vb.rlUpdatePassword, vb.rlUpdatePayPassword, vb.buttonLogout, vb.rlAddress,
            vb.rlHead, vb.rlNickName, vb.rlRealName, vb.buttonCancellation
        )
        vb.tvPhone.text = handleLoginName(UserCache.getUserInfoBean().phone)
        changeAuthStatus()

        val mUserInfoBean = UserCache.getUserInfoBean()
        GlideUtils.intoCircle(this, mUserInfoBean.avatar, vb.ivHead, R.mipmap.person_person)
        vb.tvNickName.text = mUserInfoBean.nickName

        vb.cbPush.isChecked = SPUtils.getBoolean("key_push", true)
        vb.cbPush.setOnClickListener {
            SPUtils.put("key_push", vb.cbPush.isChecked)
        }
    }

    private fun changeAuthStatus() {
        if (UserCache.getUserInfoBean().authStatus != 1) {
            vb.textRealName.text = "实名认证"
            vb.tvRealName.text = "未认证"
        } else {
            vb.textRealName.text = "修改银行卡号"
            vb.tvRealName.text = ""
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.rlRealName -> startActivityExt<RealNameAuthActivity>()
            vb.rlHead -> imageSelect.selectImage {
                isMultipleChoice = false
                onUploadSuccessListener = {
                    val headImg = it?.get(0)
                    if (headImg != null) {
                        launchUI({
                            ApiUploadFileIntercept.uploadFile("avatar", mutableListOf(headImg))
                            loginApi.updateAvatar(headImg.url).toData()
                        }, ApiObserver(mThis) {
                            setResult(Activity.RESULT_OK)
                            GlideUtils.intoCircle(mThis, headImg.path, vb.ivHead)
                            UserCache.getUserInfoBean().avatar = headImg.url
                        })
                    }
                }
            }
            vb.rlNickName -> InputDialog(mThis, "修改昵称", "请输入昵称", linsnter = { nickName ->
                if (nickName.isNotBlank()) {
                    launchUI({ loginApi.updateName(nickName).toData() }, ApiObserver(mThis) {
                        vb.tvNickName.text = nickName
                        setResult(Activity.RESULT_OK)
                        UserCache.getUserInfoBean().nickName = nickName
                    })
                }
            }).show()
            vb.rlUpdatePassword -> startActivityExt<ForgetPasswordActivity>()
            vb.rlUpdatePayPassword -> startActivityExt<SettingMoneyPasswrodActivity>()
            vb.rlAddress -> startActivityExt<ReceivingAddressActivity>()
            vb.rlRealName -> launchStartActivityForResult(RealNameAuthActivity::class.java) {
                changeAuthStatus()
            }
            vb.buttonLogout -> {
                showAlertDialog {
                    message(message = "确定退出登录吗？")
                    negativeButton { }
                    positiveButton {
                        UserCache.logout()
                        startActivityExt<LoginActivity>()
                    }
                }
            }
            vb.buttonCancellation -> {
                showAlertDialog {
                    message(message = "确定注销账号吗？")
                    negativeButton { }
                    positiveButton {
                        launchUI({
                            mallApi.accountOff().toData()
                            UserCache.logout()
                            startActivityExt<LoginActivity>()
                        }, ApiObserver(mThis))
                    }
                }
            }
        }
    }
}